package cn.iune.erp.purchase;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Aspect//指定这是一个其切面
public class PurchaseOrderDetailDaoAspect {
	
	@Autowired
	private IPurchaseService stockService;

	//指定切入点（增加）
    @Pointcut("execution(* cn.iune.erp.purchase.IPurchaseOrderDetailDao.insert(..)) && args(obj)")
    public void insertPointCut(PurchaseOrderDetail obj){}
    //指定通知以及通知的范围
    @Transactional
    @After("insertPointCut(obj)")
    public void afterInsert(PurchaseOrderDetail obj){
    	/*
		//处理关联单据（请购单）
		if (obj.getOrigin()!=null 
			&& obj.getOrigin().equals("QGDJ") 
			&& obj.getOriginNo()!=null 
			){
			//更新请购单的正在采购数量
			purchaseAskforDetailService.updateStoreItemQuantityIng(obj.getOriginNo(),obj.getEnterpriseNo(),obj.getItemAmount());
		}
		//处理关联单据（采购计划）
		if (obj.getOrigin()!=null 
			&& obj.getOrigin().equals("XQJH") 
			&& obj.getOriginNo()!=null 
			){
			//更新请购单的正在采购数量
//			requirePlanUtil.updatePickItemIng(obj.getOriginNo(),obj.getFactoryNo(),obj.getItemNo(),obj.getEnterpriseNo(),obj.getItemAmount());
		}
    	*/
    	stockService.afterInsertOrderDetail(obj);
    }
    
	//指定切入点（删除）
    @Pointcut("execution(* cn.iune.erp.purchase.IPurchaseOrderDetailDao.delete(..)) && args(fID)")
    public void deletePointCut(String fID){}
    //指定通知以及通知的范围
    @Transactional
    @Before("deletePointCut(fID)")
    public void beforeDelete(String fID){
    	/*
		PurchaseOrderDetail obj = purchaseOrderDetailService.getByID(fID);
		//处理关联单据（请购单）
		if (obj.getOrigin()!=null 
			&& obj.getOrigin().equals("QGDJ") 
			&& obj.getOriginNo()!=null 
			){
			//更新请购单的正在采购数量
			purchaseAskforDetailService.updateStoreItemQuantityIng(obj.getOriginNo(),obj.getEnterpriseNo(),obj.getItemAmount().negate());
		}
		//处理关联单据（采购计划）
		if (obj.getOrigin()!=null 
			&& obj.getOrigin().equals("XQJH") 
			&& obj.getOriginNo()!=null 
			){
			//更新请购单的正在采购数量
//			requirePlanUtil.updatePickItemIng(obj.getOriginNo(),obj.getFactoryNo(),obj.getItemNo(),obj.getEnterpriseNo(),obj.getItemAmount().negate());
		}
    	*/
    	stockService.beforeDeleteOrderDetail(fID);
    }
    
	//指定切入点（修改）
    @Pointcut("execution(* cn.iune.erp.purchase.IPurchaseOrderDetailDao.update(..)) && args(obj)")
    public void updatePointCut(PurchaseOrderDetail obj){}
    //指定通知以及通知的范围
    @Transactional
    @Around("updatePointCut(obj)")
     public PurchaseOrderDetail aroundUpdate(JoinPoint joinPoint, PurchaseOrderDetail obj) throws Throwable{
         System.out.println("环绕通知上。。。");
         stockService.beforeDeleteOrderDetail(obj.getfID());
         PurchaseOrderDetail objNew = (PurchaseOrderDetail) ((ProceedingJoinPoint) joinPoint).proceed();
         System.out.println("环绕通知下。。。");
         stockService.afterInsertOrderDetail(objNew);
         return objNew;
     }
    
	
}
